package ml.uuku.swipeheadertabs.adapter;

import java.util.List;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 供RecyclerView使用的简单字符串适配器，支持点击事件
 * 
 * @author <a href=http://my.oschina.net/uuku>uuku</a>
 * @version V0.1
 */
public class SimpleStringListAdapter extends RecyclerView.Adapter<SimpleStringListAdapter.ViewHolder>
        implements View.OnClickListener {

    private List<String> mItems;
    private OnItemClickListener onItemClickListener;

    public SimpleStringListAdapter(List<String> items) {
        mItems = items;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void appendItems(List<String> items) {
        if (items != null && items.size() > 0) {
            this.mItems.addAll(items);
            notifyDataSetChanged();
        }
    }

    public void resetItems(List<String> items) {
        if (items != null && items.size() > 0) {
            this.mItems.clear();
            this.mItems.addAll(items);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, null);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String data = mItems.get(position);
        holder.textView.setText(data);
        holder.container.setTag(data);
        data = null;
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public void onClick(View v) {

        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(v, v.getTag());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public final TextView textView;
        public final View container;

        public ViewHolder(View itemView) {
            super(itemView);
            container = itemView;
            textView = (TextView) itemView.findViewById(android.R.id.text1);
        }

    }

    public interface OnItemClickListener {

        /**
         * 在点击Item时所触发的回调
         * 
         * @param view 当前点击的View
         * @param data 与当前点击的View相关联的数据(简单起见，目前数据来自于View的tag)
         * @return
         */
        boolean onItemClick(View view, Object data);
    }
}
